package com.wzy.new_energy_car_platform.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @author
 * @email ymz@ebox.vip
 * @date 2023/2/26 14:17
 * @desciption: 全局统一返回结果类
 */
@Data
@Builder
@AllArgsConstructor
public class ResultDto<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 返回码
     */
    private ResultCode code;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 数据数
     */
    private long total;
    /**
     * 返回数据
     */
    private T data;

    public ResultDto(){this.total = Long.valueOf(0L);}

    public ResultDto(ResultCode code){this.code = code;}

    public ResultDto(ResultCode code, T data){
        super();
        this.code = code;
        this.data = data;
        this.message = code.getMessage();
    }

    public String getCode(){
        return this.code.getCode();
    }

    public String getCodeMessage(){
        return this.code.getMessage();
    }
    public static  <T> ResultDto<T> ok(){
        return new ResultDto<T>(ResultCode.OK);
    }

    public static <T> ResultDto ok(T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .message(ResultCode.OK.getMessage())
                .build();
    }

    public static <T> ResultDto ok(T data, long total) {
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .total(total)
                .message(ResultCode.OK.getMessage())
                .build();
    }

    public static <T> ResultDto ok(String message){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .build();
    }

    public static <T> ResultDto ok(String message, T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .data(data)
                .build();
    }

    public static <T> ResultDto error(){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(ResultCode.ERROR.getMessage())
                .data(null)
                .build();
    }

    public static <T> ResultDto error(String message){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(message == null || message.isEmpty() ? ResultCode.ERROR.getMessage() : message)
                .data(null)
                .build();
    }

    public boolean isSuccess(){
        if (this.getCode() == ResultCode.OK.name()){
            return true;
        }
        return false;
    }
}